package com.yuexun.beilunpatient.ui.registration.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yuexun.beilunpatient.R;
import com.yuexun.beilunpatient.base.BaseEntity;
import com.yuexun.beilunpatient.config.AppConfig;
import com.yuexun.beilunpatient.ui.registration.adapter.RegistrationHosAdapter;
import com.yuexun.beilunpatient.ui.registration.bean.AppointmentHospitalBean;
import com.yuexun.beilunpatient.ui.registration.model.impl.RegistrationModel;
import com.yuexun.beilunpatient.ui.registration.presenter.impl.AppointmentHospitalPresenter;
import com.yuexun.beilunpatient.ui.registration.ui.Act_Registration_Department;
import com.yuexun.beilunpatient.ui.registration.ui.view.IAppointmentHospitalView;
import com.yuexun.beilunpatient.widget.EmptyLayout;

import org.kymjs.kjframe.ui.KJFragment;
import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by chenss on 2018/3/20 0020.
 */

public class Frag_Registration_Hospital extends KJFragment implements IAppointmentHospitalView {
    @Bind(R.id.content_list)
    ListView pullList;
    @Bind(R.id.empty_layout)
    EmptyLayout emptyLayout;
    @Bind(R.id.tv_hos)
    TextView tvHos;
    @Bind(R.id.iv_hos)
    ImageView ivHos;
    @Bind(R.id.tv_dep)
    TextView tvDep;
    @Bind(R.id.iv_dep)
    ImageView ivDep;
    @Bind(R.id.rl_hos)
    RelativeLayout rlHos;
    @Bind(R.id.rl_dep)
    RelativeLayout rlDep;

    RegistrationHosAdapter adapter;
    ArrayList<AppointmentHospitalBean> hospitalBeans = new ArrayList<>();
    AppointmentHospitalPresenter hospitalPresenter;

    @Override
    protected View inflaterView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
        View view = inflater.inflate(R.layout.frag_registration_hospital, null);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initData() {
        hospitalPresenter = new AppointmentHospitalPresenter(new RegistrationModel(), this);

        adapter = new RegistrationHosAdapter(pullList, hospitalBeans, R.layout.item_registrationhos);
        pullList.setAdapter(adapter);

        emptyLayout.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                emptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
                ApiGetHos();
            }
        });

        ApiGetHos();

        pullList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                AppointmentHospitalBean item = (AppointmentHospitalBean) adapterView.getItemAtPosition(i);
                Bundle bundle = new Bundle();
                bundle.putSerializable("hosBean", item);
                Intent intent = new Intent(getActivity(), Act_Registration_Department.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    private void ApiGetHos(){
        Map<String, String> params = new HashMap<>();
        params.put("areaCode", "330206");
        params.put("hospName", "");
        params.put("type", "");
        params.put("sessionUuidPatient", PreferenceHelper.readString(getActivity(), AppConfig.USER_INFO, AppConfig.SESSION_ID));
        hospitalPresenter.appointmenthospitalList(params);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void appointmenthospitalList(BaseEntity<AppointmentHospitalBean> baseEntity) {
        if(baseEntity.getStatus().equals("200")){
            hospitalBeans = baseEntity.getDatas();
            adapter.refresh(hospitalBeans);
        }else {
            ViewInject.toast(baseEntity.getMessage());
        }

        if (hospitalBeans.size() == 0) {
            emptyLayout.setErrorType(EmptyLayout.NODATA);
        } else {
            emptyLayout.dismiss();
        }
    }

}
